package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.activity.VideoPlayerVRActivity;
import com.v1.video.activity.WebViewActivity3;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.domain.ThemeDetailEntity.ThematicInfo;
import com.v1.video.util.Helper;
import com.v1.video.util.Utility;

import java.util.ArrayList;
import java.util.List;

public class ThemeDetailAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private List<ThematicInfo> mLstData;

	public ThemeDetailAdapter(Context context) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mLstData = new ArrayList<ThematicInfo>();
	}

	public List<ThematicInfo> getLstData() {
		return mLstData;
	}

	public void setLstData(List<ThematicInfo> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}
	}
	
	public void setAddLstData(List<ThematicInfo> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData.addAll(mLstData);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.activity_buddhismdetail_item, null);
			holder = new Holder();
			holder.layItem = (RelativeLayout) convertView
					.findViewById(R.id.lay_item);
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			holder.time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final ThematicInfo info = mLstData.get(position);
		if (!TextUtils.isEmpty(info.getTitle())) {
			holder.title.setText(info.getTitle());
		} else {
			holder.title.setText("");
		}
		if (!TextUtils.isEmpty(info.getDuration())) {
			holder.time.setText(Utility.getDuration(info.getDuration()));
		} else {
			holder.time.setText("00:00");
		}
		if (TextUtils.isEmpty(info.getPic())) {
			holder.image
					.setImageResource(R.drawable.common_place_holder_bg_16_10);
		} else {
			ImageLoader.getInstance().displayImage(info.getPic(), holder.image,
					Constant.VIDEO_10_16_OPTION, null);
		}
		holder.layItem.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(mContext)) {
					if (info != null) {
						Intent intent = new Intent();
						ScenarioVideoInfo videoInfo = new ScenarioVideoInfo(
								info.getVid());
						if (videoInfo != null) {
							if (!TextUtils.isEmpty(info.getAt())) {
								if (info.getAt().equals(Constant.VIDEO_TYPE_4)) {
									intent.setClass(mContext,
											WebViewActivity3.class);
									intent.putExtra("adLink",
											info.getVideoUrl());
									intent.putExtra(
											WebViewActivity3.FLAG_TITLE,
											info.getTitle());
								} else if (info.getAt().equals(
										Constant.VIDEO_TYPE_6)) {
									if (info.getVideoUrl() != null
											&& info.getVideoUrl().length() >= 6) {
										intent.setClass(mContext,
												VideoPlayerVRActivity.class);
										intent.putExtra("videourl",
												info.getVideoUrl());
									} else {
										intent.setClass(mContext,
												PaikeVideoDetailActivity2.class);
										intent.putExtra("videoInfo", videoInfo);
										intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
									}
								} else if (info.getAt().equals(
										Constant.VIDEO_TYPE_1)) {
									intent.setClass(mContext,
											PaikeVideoDetailActivity2.class);
									intent.putExtra("videoInfo", videoInfo);
									intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
								}
							} else {
								intent.setClass(mContext,
										PaikeVideoDetailActivity2.class);
								intent.putExtra("videoInfo", videoInfo);
								intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
							}
							mContext.startActivity(intent);
						}
					}
				} else {
					Toast.makeText(
							mContext,
							mContext.getResources().getString(
									R.string.net_no_notclick),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		return convertView;
	}

	private class Holder {
		public RelativeLayout layItem;
		public ImageView image;
		public TextView time;
		public TextView title;
	}
}
